package com.smartian.v2.impl;

import android.text.TextUtils;


import com.smartian.v2.AbstractTextParser;
import com.smartian.v2.OpLog;
import com.smartian.v2.TextPattern;
import com.smartian.v2.filters.TextParserInterceptorManager;
import com.smartian.v2.model.DateTimeNode;
import com.smartian.v2.model.ScheduleTaskBean;
import com.smartian.v2.utils.DateTimeHelper;
import com.smartian.v2.utils.FestivalFilter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * Created by tianweitong on 2020/3/13.
 */
public class YearlyTextParser extends AbstractTextParser {

    private int mFrequent = 1;
    private volatile boolean mHasParsed = false;
    private List<DateTimeNode> mDateTimeNodeList = new ArrayList<>();

    public YearlyTextParser(String sourceText, String repeatText, int repeatTextIndex) {
        super(sourceText, repeatText, repeatTextIndex);
    }

    @Override
    public ScheduleTaskBean getScheduleTaskBean() {

        startHandler();

        if(mDateTimeNodeList!=null) {
            Collections.sort(mDateTimeNodeList);
        }
        OpLog.d("TextParser",mDateTimeNodeList+"");

        DateTimeNode dateNode = DateTimeHelper.findBestDateNode(mDateTimeNodeList,DateTimeNode.TYPE_DATE);
        DateTimeNode timeNode = DateTimeHelper.findBestDateNode(mDateTimeNodeList,DateTimeNode.TYPE_TIME);

        if(dateNode==null || timeNode==null){
            return null;
        }
        if(TextUtils.isEmpty(dateNode.resultText) || TextUtils.isEmpty(timeNode.resultText)){
            return null;
        }

        List<DateTimeNode> dateTimeNodes  = new ArrayList<>();

        dateTimeNodes.add(dateNode);
        dateTimeNodes.add(timeNode);

        String date = dateNode.resultText;
        String time = timeNode.resultText;
        String dateTime = formatYearDateTime(date,time);

        ScheduleTaskBean scheduleTaskBean = new ScheduleTaskBean();
        scheduleTaskBean.setFraquent(mFrequent);
        scheduleTaskBean.setRepeatType(TextPattern.REPEAT_YEARLY);
        scheduleTaskBean.setRepeatText(getRepeatText());
        scheduleTaskBean.setDateTime(dateTime);
        scheduleTaskBean.setDateTimeFormat(getDateTimeFormat());
        scheduleTaskBean.setSourceText(getSourceText());
        scheduleTaskBean.setDateTimeNodes(dateTimeNodes);

        return scheduleTaskBean;
    }

    @Override
    public boolean isValid() {
        startHandler();
        return mFrequent>0;
    }

    private void startHandler() {
        if(!mHasParsed) {
            List<DateTimeNode> dateTimeNodes = startParse();
            if (dateTimeNodes != null) {
                mDateTimeNodeList.clear();
                dateTimeNodes = TextParserInterceptorManager.dispatchIntercept(this,TextPattern.REPEAT_YEARLY,dateTimeNodes);
                mDateTimeNodeList.addAll(dateTimeNodes);
            }
            mHasParsed = true;
        }
    }

    private List<DateTimeNode> startParse() {

        List<DateTimeNode> dateTimeNodes = new ArrayList<>();
        String sourceText = getSourceText();
        if(TextUtils.isEmpty(sourceText)) return dateTimeNodes;

        mFrequent = parseFrequent();
        if(mFrequent<0) return dateTimeNodes;

        List<DateTimeNode> dateNodes = getParseDate();
        List<DateTimeNode> festivalDateNodes = FestivalFilter.getInstance().getFilterDateNodes(sourceText);
        if(festivalDateNodes!=null){
            dateNodes.addAll(festivalDateNodes);
            Collections.sort(dateNodes);
        }

        List<DateTimeNode> timeNodes = getParseTime();

        if(dateNodes.size()==0){

            DateTimeNode timeNode = new DateTimeNode();
            timeNode.text = "";
            timeNode.index = 0;
            timeNode.resultText = DateTimeHelper.getToday();
            timeNode.locatorText = "";
            timeNode.type = DateTimeNode.TYPE_DATE;
            dateNodes.add(timeNode);
        }

        if(timeNodes.size()==0) {
            String currentTime = DateTimeHelper.getCurrentTime();
            DateTimeNode dateTimeNode = new DateTimeNode();
            dateTimeNode.text = "";
            dateTimeNode.index = 0;
            dateTimeNode.resultText = currentTime;
            dateTimeNode.locatorText = "";
            dateTimeNode.type = DateTimeNode.TYPE_TIME;
            timeNodes.add(dateTimeNode);
        }
        if(dateNodes!=null) {
            dateTimeNodes.addAll(dateNodes);
        }
        if(timeNodes!=null) {
            dateTimeNodes.addAll(timeNodes);
        }

        if(dateTimeNodes.size()>0){
            Collections.sort(dateTimeNodes);
        }

        return dateTimeNodes;

    }

    private static String formatYearDateTime(String date, String time) {
        if(TextUtils.isEmpty(date) && TextUtils.isEmpty(time)) return null;

        Calendar calendar = Calendar.getInstance();
        if(!TextUtils.isEmpty(date)) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            try {
                Date targretTimes = simpleDateFormat.parse(date);
                Calendar target = Calendar.getInstance();
                target.setTimeInMillis(targretTimes.getTime());
                calendar.set(Calendar.MONTH,target.get(Calendar.MONTH));
                calendar.set(Calendar.DAY_OF_MONTH,target.get(Calendar.DAY_OF_MONTH));
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }

        if(!TextUtils.isEmpty(time)) {
            String[] timeParts = time.split(TextPattern.TIME_SEPATOR);
            if (timeParts != null && timeParts.length == 2) {

                int hour = DateTimeHelper.getIntegerValue(timeParts[0]);
                int min = DateTimeHelper.getIntegerValue(timeParts[1]);

                calendar.set(Calendar.HOUR_OF_DAY, hour);
                calendar.set(Calendar.MINUTE, min);

            }

        }

        return TextPattern.sYearsTimeFormat.format(calendar.getTimeInMillis());
    }

    @Override
    public String getDateTimeFormat() {
        return "MM-dd HH:mm";
    }
}
